const { ipcMain, BrowserWindow, shell } = require('electron')
const WinState = require('electron-win-state').default
const saveImg = require('./saveImg.js')
const path = require('path')

const openWindow = (url) => {
    const winState = new WinState({
        defaultHeight: 1080,
        defaultWidth: 1920,
        electronStoreOptions: {
            name: 'window-state-open'
        }
    })
    const win = new BrowserWindow({
        ...winState.winOptions,
        show: false,
        icon: path.resolve(__dirname, '../icon.ico'),
        webPreferences: {
            preload: path.resolve(__dirname, '../preload/index.js')
        }
    })


    win.on('ready-to-show', () => {
        win.show()
    })

    win.loadURL(url)

    winState.manage(win)

    // win.webContents.openDevTools()

    win.webContents.on('context-menu', (e, args) => {
        saveImg(args.srcURL, win)
    })
}

// ipcMain.handle('on-openWindow-event', (e, url) => openWindow(url))
ipcMain.handle('on-openWindow-event', (e, url) => shell.openExternal(url))
